package com.fone.player.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.activity.ShareEditActivity;
import com.fone.player.activity.main.MainTabActivity;
import com.fone.player.activity.main.SplashActivity;
import com.fone.player.cache.ImageDownloadModule;
import com.fone.player.client.Reporter;
import com.fone.player.sns.ISNSShareManager;
import com.fone.player.sns.ISNSShareManager.ShareStateListener;
import com.fone.player.sns.bean.ShareInfo;
import com.fone.player.sns.impl.SNSShareManager;
import com.fone.player.util.ActivityQueue;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.wxapi.WXEntryActivity;

@SuppressWarnings("unused")
public class SelectSharePopwindow implements OnClickListener {

	private static final String TAG = "SelectSharePopwindow";
	private PopupWindow mPopupWindow;
	private int height;
	private int width;
	private View popupWindowView;
	private ImageView winxinFriend, winxin, sina, tencent, qq;
	private ShareInfo mShareInfo;
	private Button cancelBt;

	private SNSShareManager mSNSShareManager;
	private ShareStateListener mShareStateListener;

	private String select_share_link_address;// 分享标头链接
	private String select_share_word_suffix;// 分享内容后缀
	private String share_word_title_prefix;// 分享前缀

	@SuppressWarnings("deprecation")
	public SelectSharePopwindow(Activity activity, int height, int width, ShareStateListener shareStateListener) {
		this.height = height;
		this.width = width;
		select_share_link_address = activity.getApplicationContext().getResources().getString(R.string.select_share_link_address);
		select_share_word_suffix = activity.getApplicationContext().getResources().getString(R.string.select_share_word_suffix);
		share_word_title_prefix = activity.getApplicationContext().getResources().getString(R.string.select_share_word_title_prefix);

		LayoutInflater layoutInflater = (LayoutInflater) activity.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		popupWindowView = layoutInflater.inflate(R.layout.select_share_platform_popwindow, null);
		mPopupWindow = new PopupWindow(popupWindowView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

		mPopupWindow.setAnimationStyle(R.style.anim_share_select);
		mPopupWindow.setWidth(this.width);
		mPopupWindow.setHeight(this.height);

		mPopupWindow.setFocusable(true);
		mPopupWindow.setOutsideTouchable(true);

		mShareStateListener = shareStateListener;
		mSNSShareManager = SNSShareManager.getInstance();
		mSNSShareManager.setShareStateListener(mShareStateListener);
		iniView();
	}

	private void iniView() {
		winxinFriend = (ImageView) popupWindowView.findViewById(R.id.share_weixin_friend_iv);
		winxin = (ImageView) popupWindowView.findViewById(R.id.share_weixin_iv);
		sina = (ImageView) popupWindowView.findViewById(R.id.share_sina_iv);
		tencent = (ImageView) popupWindowView.findViewById(R.id.share_tencent_iv);
		qq = (ImageView) popupWindowView.findViewById(R.id.share_qq_iv);

		cancelBt = (Button) popupWindowView.findViewById(R.id.select_share_cancel);

		winxinFriend.setOnClickListener(this);
		winxin.setOnClickListener(this);
		sina.setOnClickListener(this);
		tencent.setOnClickListener(this);
		qq.setOnClickListener(this);
		cancelBt.setOnClickListener(this);

	}

	/**
	 * 弹出popwindow
	 * 
	 * @param view
	 *            以这个布局的右下角为基准
	 * @param shareInfo
	 *            分享信息
	 */
	public void show(View view, ShareInfo shareInfo) {

		if (FoneUtil.isNetOkWithToast(ApplicationManage.getAplicationManageInstance()) && mPopupWindow != null && shareInfo != null) {
			// 分享按钮点击的数据上报
			L.v("Reporter", "logShare", " cid : " + shareInfo.countID + " state : " + 0 + " comeFrom : " + shareInfo.comeFrom);
			if (shareInfo.comeFrom != 0) {
				Reporter.logShare(String.valueOf(shareInfo.countID), 0, shareInfo.comeFrom);
			}
			mShareInfo = shareInfo;
			mPopupWindow.showAtLocation(view, Gravity.LEFT | Gravity.BOTTOM, 0, 0);
		}
	}

	/**
	 * 隐藏popwindow
	 */
	public void dismiss() {
		if (mPopupWindow != null && mPopupWindow.isShowing()) {
			mPopupWindow.dismiss();
		}
	}

	public View getContentView() {
		if (mPopupWindow != null) {
			return mPopupWindow.getContentView();
		}
		return null;
	}

	public PopupWindow getPopupWindow() {
		return mPopupWindow;
	}

	public boolean isShowing() {
		if (mPopupWindow != null) {
			return mPopupWindow.isShowing();
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		if (!FoneUtil.isNetOkWithToast(ApplicationManage.getAplicationManageInstance())) {
			return;
		}
		integrateShareInfo(mShareInfo);
		switch (v.getId()) {
		case R.id.share_weixin_friend_iv:
			mShareInfo.SNSType = ISNSShareManager.TENCENT_WEIXIN_FRIEND_TYPE;
			L.v(TAG, "onClick", "share_weixin_friend_iv");
			if (TextUtils.isEmpty(mShareInfo.imagePath) && !TextUtils.isEmpty(mShareInfo.imageUrl)) {
				String imagePath = ImageDownloadModule.getInstance().getImagePathByUrl(mShareInfo.imageUrl);
				if (!TextUtils.isEmpty(imagePath)) {
					mShareInfo.imagePath = imagePath;
				}
			}
			// TODO 添加固定分享前后缀
			mShareInfo.title = share_word_title_prefix + mShareInfo.title;
			mShareInfo.text = mShareInfo.text + "," + mShareInfo.videoUrl + "," + select_share_word_suffix;

			WXEntryActivity.countID = mShareInfo.countID;
			WXEntryActivity.comeFrom = mShareInfo.comeFrom;

			mSNSShareManager.shareMessage(mShareInfo);
			break;
		case R.id.share_weixin_iv:
			L.v(TAG, "onClick", "share_weixin_iv");
			mShareInfo.SNSType = ISNSShareManager.TENCENT_WEIXIN_TYPE;
			if (TextUtils.isEmpty(mShareInfo.imagePath) && !TextUtils.isEmpty(mShareInfo.imageUrl)) {
				String imagePath = ImageDownloadModule.getInstance().getImagePathByUrl(mShareInfo.imageUrl);
				if (!TextUtils.isEmpty(imagePath)) {
					mShareInfo.imagePath = imagePath;
				}
			}
			// TODO 添加固定分享前后缀
			mShareInfo.title = share_word_title_prefix + mShareInfo.title;
			mShareInfo.text = mShareInfo.text + "," + mShareInfo.videoUrl + "," + select_share_word_suffix;

			WXEntryActivity.countID = mShareInfo.countID;
			WXEntryActivity.comeFrom = mShareInfo.comeFrom;

			mSNSShareManager.shareMessage(mShareInfo);
			break;
		case R.id.share_sina_iv:
			L.v(TAG, "onClick", "share_sina_iv");
			mShareInfo.SNSType = ISNSShareManager.SINA_WEIBO_TYPE;
			// TODO 添加固定分享title前缀
			mShareInfo.title = share_word_title_prefix + mShareInfo.title;
			openEditPage(mShareInfo);
			break;
		case R.id.share_tencent_iv:
			L.v(TAG, "onClick", "share_tencent_iv");
			mShareInfo.SNSType = ISNSShareManager.TENCENT_WEIBO_TYPE;
			if (TextUtils.isEmpty(mShareInfo.imagePath) && !TextUtils.isEmpty(mShareInfo.imageUrl)) {
				String imagePath = ImageDownloadModule.getInstance().getImagePathByUrl(mShareInfo.imageUrl);
				if (!TextUtils.isEmpty(imagePath)) {
					mShareInfo.imagePath = imagePath;
				}
			}
			// TODO 添加固定分享title前缀
			mShareInfo.title = share_word_title_prefix + mShareInfo.title;
			openEditPage(mShareInfo);
			break;
		case R.id.share_qq_iv:
			L.v(TAG, "onClick", "share_qq_iv");
			mShareInfo.SNSType = ISNSShareManager.TENCENT_QQ_TYPE;
			mShareInfo.text = "我正在使用100tv，快来跟我一起用吧。";
			openEditPage(mShareInfo);
			break;
		case R.id.select_share_cancel:
			L.v(TAG, "onClick", "select_share_cancel");
			break;

		default:
			break;
		}
		dismiss();
	}

	private void openEditPage(ShareInfo shareInfo) {
		Activity activity = ActivityQueue.popIndex(0);
		if (activity != null) {
			Intent intent = new Intent(activity, ShareEditActivity.class);
			if (TextUtils.isEmpty(shareInfo.text)) {
				shareInfo.text = "我正在使用100tv，快来跟我一起用吧。";
			}
			Bundle bundle = new Bundle();
			bundle.putSerializable("shareInfo", shareInfo);
			intent.putExtras(bundle);

			activity.startActivity(intent);
		}
	}

	/**
	 * 整合ShareInfo分享信息
	 * 
	 * @param shareInfo
	 * @return
	 */
	private void integrateShareInfo(ShareInfo mShareInfo) {
		L.v(TAG, "integrateShareInfo", "mShareInfo.videoUrl : " + mShareInfo.videoUrl);
		if (TextUtils.isEmpty(mShareInfo.videoUrl)) {
			mShareInfo.videoUrl = select_share_link_address;
		}
		mShareInfo.titleUrl = mShareInfo.videoUrl;
		mShareInfo.site = "100tv聚好看";
		mShareInfo.siteUrl = select_share_link_address;

	}

}
